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dmIRgYwHx+cEjzJAQSQkd0G3PSfJLBaf/0Vo1nBav8q9
VjgqhEFaNTzEj5Hqn6ldwKUul4Vb+AoSiz5Z1du32ul1CtcozUJTcWJL9ebZ8YbS
qy7Ol4slSW3ukNaG6tBTqQYb9liIdvdQUG6oJhmzbwARAQABiQIlBBgBCgAPAhsM
BQJXyXjnBQkMrEdiAAoJEM37X6UgB7lUJ9YP+gIFmDyjBdTD+pX+6z9FBb8p7SF8
hLEpP51m1CSAfquDfaMOVRQ6d4P3N0KjPQs7uz1uqjQliyaek4jtEqMfkPdYwcvj
8Zv17u41fAkS3BJFg3cR2+jtzF69sA/phHR+/a6MSnm/W+0YFOQHBKO6MIwC0an5
qHzG1MBic6rb8qjZlsJowwlF06ZrSmodhulB2q2JMAqUvQd923wJtMIlur4wavpn
iKdCYomlNcPF/UMVutUn0XuP7ca0yFXJgKJznLAFQ8g+b8aLyvodZYHjblaizkoj
EWDN3zPTQebnP7N8wTl0+rc2F3Bmnak6+m0P/F1kYXyz3obSvQXwe3rYAkxarcg9
5wXQRn6hN0PBb5lD05Ytx/peZrYpYFe4CYPtiL+y7sseExQhrRjpWr3FKGCKBpsr
QkGUk3MiZ4iHVvlyKv/Z9IMYNRmVzMXUM2QAbRgYD0x4kvdXso6YR2rn8sC89c/O
xIBK1HRwVgfRofUe9PmdeKhsBVYEQkNb8V18YP0GswrBc4cjoGqDwV64yPAotP2t
B9QhqjJFM9xDT/wJSxKi1uzzSwQBfpD/ylsTB6/v1K3dksvQ6cVZqaBt4g2hq78r
Eq+ZyncUD2z3eXXrlVheqJdXZXoayQFaTo4iIAc7wbraZZ6mDr6mxxFMlUqO2qxT
h3RIcafYvaF7yUDA
=hQKe
-----END PGP PUBLIC KEY BLOCK-----
EOF
}

install_deb() {
  LIST_FILE=/etc/apt/sources.list.d/metasploit-framework.list
  PREF_FILE=/etc/apt/preferences.d/pin-metasploit.pref
  if [ ! -f $LIST_FILE ]; then
    echo -n "Adding metasploit-framework to your repository list.."
    echo "deb $DOWNLOAD_URI/apt lucid main" > $LIST_FILE
    print_pgp_key | apt-key add -
  fi
  if [ ! -f $PREF_FILE ]; then
    mkdir -p /etc/apt/preferences.d/
    cat > $PREF_FILE <<EOF
Package: metasploit*
Pin: origin downloads.metasploit.com
Pin-Priority: 1000
EOF
  fi
  echo -n "Updating package cache.."
  apt-get update > /dev/null
  echo "OK"
  echo "Checking for and installing update.."
  apt-get install -y --allow-downgrades metasploit-framework
}

install_rpm() {
  echo "Checking for and installing update.."
  REPO_FILE=/etc/yum.repos.d/metasploit-framework.repo
  GPG_KEY_FILE=/etc/pki/rpm-gpg/RPM-GPG-KEY-Metasploit
  if [ ! -f $REPO_FILE ]; then
    echo -n "Adding metasploit-framework to your repository list.."

    cat > /etc/yum.repos.d/metasploit-framework.repo <<EOF
[metasploit]
name=Metasploit
baseurl=$DOWNLOAD_URI/rpm
gpgcheck=1
gpgkey=file://$GPG_KEY_FILE
enabled=1
EOF
    print_pgp_key > ${GPG_KEY_FILE}
  fi
  yum install -y metasploit-framework
}

install_pkg()
{
  (
    cd ~/Downloads

    echo "Downloading package..."
    curl -O "$DOWNLOAD_URI/osx/metasploitframework-latest.pkg"

    echo "Checking signature..."

    if pkgutil --check-signature metasploitframework-latest.pkg; then
      echo "Installing package..."
      installer -pkg metasploitframework-latest.pkg -target /
    fi

    echo "Cleaning up..."
    rm -fv metasploitframework-latest.pkg
  )
}

DOWNLOAD_URI=http://downloads.metasploit.com/data/releases/metasploit-framework
PKGTYPE=unknown
ID=`id -u`

if [ -f /etc/redhat-release ] ; then
  PKGTYPE=rpm
elif [ -f /etc/system-release ] ; then
  # If /etc/system-release is present, this is likely a distro that uses RPM.
  PKGTYPE=rpm
else
  if uname -sv | grep 'Darwin' > /dev/null; then
    PKGTYPE=pkg
  else
    PKGTYPE=deb
  fi
fi

if [ "$ID" -ne 0 ]; then
  if ! hash sudo 2>/dev/null; then
    echo "This script must be executed as the 'root' user or with sudo"
    exit 1
  else
    echo "Switching to root user to update the package"
    sudo -E $0 $@
    exit 0
  fi
fi

case $PKGTYPE in
  deb)
    install_deb
    ;;
  rpm)
    install_rpm
    ;;
  *)
    install_pkg
esac
